<template>
  <BasicForm @register="registerForm"/>
  <!-- 表类型: 主子表 -->
  <!-- <BasicForm @register="registerSubForm" v-if="tableCategory === 'sub'"/> -->
  <!-- 表类型: 树型表 -->
  <!-- <BasicForm @register="registerTreeForm" v-if="tableCategory === 'tree'"/> -->
</template>

<script lang="ts" setup>
import {BasicForm, useForm} from '/@/components/Form';
import {genInfoFormSchema} from '../../codeGen.data';
import {ref, toRefs, watch} from 'vue';

const props = defineProps({
    // 子组件接收父组件传递过来的值
    genTable: Object,
  })

  const {genTable} = toRefs(props);

  const tableCategory = ref('')

  const [registerForm, {resetFields, setFieldsValue, validate, updateSchema}] = useForm({
    schemas: genInfoFormSchema,
    showActionButtonGroup: false,
    baseColProps: {lg: 10, sm: 24} 
  });

  const getData = async () => {
    const baseValue = await validate();
    // const treeTableValue = await treeValidate();
    // const subTableValue = await subValidate();
    // return {...baseValue, ...treeTableValue, ...subTableValue}
    return {...baseValue}
  }
  
  watch(
    () => props.genTable,
    async  (val) => {
      try {
        if (genTable) {
          // 重置表单
          await resetFields();
          //表单赋值
          await setFieldsValue({
            ...val,
          });
        }
      } catch(err) {
        console.error(err)
      }
    },
    { deep: true, immediate: true }
  )

  defineExpose({getData})
</script>
